package com.easylinkin.linkappapi.reportCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.reportCenter.entity.ReportAiDetail;
import com.easylinkin.linkappapi.reportCenter.entity.ReportElectrical;
import com.easylinkin.linkappapi.reportCenter.entity.ReportMachinery;
import com.easylinkin.linkappapi.reportCenter.entity.dto.ReportDetailDTO;
import com.easylinkin.linkappapi.reportCenter.entity.vo.ReportAiVo;
import com.easylinkin.linkappapi.reportCenter.entity.vo.ReportMachineryVo;
import com.easylinkin.linkappapi.reportCenter.mapper.ReportMachineryMapper;
import com.easylinkin.linkappapi.reportCenter.service.ReportMachineryService;
import com.easylinkin.linkappapi.reportCenter.timer.DeviceReportTimer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description 塔吊/升降机统计
 * @Author kan yuanfeng
 * @Date 2023/7/17 14:27
 */
@Service
public class ReportMachineryServiceImpl extends ReportBaseServiceImpl<ReportMachineryVo> implements ReportMachineryService {
    @Autowired
    private ReportMachineryMapper reportMachineryMapper;
    @Autowired
    private DeviceReportTimer deviceReportTimer;

    @Override
    protected List<ReportMachineryVo> getByReportId(Integer reportId,Integer type) {
        QueryWrapper<ReportMachinery> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_id_",reportId).eq("type_",type);
        List<ReportMachinery> reportMachineryList = reportMachineryMapper.selectList(queryWrapper);
        List<ReportMachineryVo> machineryVos = BeanUtil.copyToList(reportMachineryList, ReportMachineryVo.class);
        return machineryVos;
    }

    @Override
    protected List<ReportMachineryVo> getLastByDate(String tenantId,Date date,Integer type) {
        //根据时间获取ai预警
        List<ReportMachinery> machineryList = deviceReportTimer.countMachinery(tenantId, date, type, null);
        List<ReportMachineryVo> machineryVos = BeanUtil.copyToList(machineryList, ReportMachineryVo.class);
        return machineryVos;
    }

    /**
     * 当月数据处理
     * @param reportId
     * @param name
     * @param type (两个数据在一张表时才有)
     * @return
     */
    @Override
    public ReportDetailDTO dataHandler(Integer reportId, String name, Integer type) {
        ReportDetailDTO reportDetailDTO = this.dataHandler(reportId, name, type, (cur, last) -> {
            //判断是不是汇总数据
            if (StringUtils.isBlank(last.getDeviceCode()) && StringUtils.isBlank(cur.getDeviceCode())){
                cur.setLastHour(last.getHours());
                cur.computeRatioHour();
            }
            cur.setLastAlarmNum(last.getAlarmNum());
            cur.computeRatioAlarm();
        });
        return reportDetailDTO;
    }
}
